//
//  MYMyCollectNewsListVC.m
//  MuYing
//
//  Created by tbb on 2019/8/8.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYMyCollectNewsListVC.h"
#import "MYNewsDetailsHotelVC.h"
#import "YQPNewsListModel.h"
#import "MYNewsDetailsHotTopicVC.h"
#import "MYNewsDetailsActionVC.h"
#import "MYNewsDetailsScenicspotVC.h"
@interface MYMyCollectNewsListVC ()

@property (strong, nonatomic) NSMutableArray *newsArray;

@property (strong, nonatomic) YQPNewsListModel *model;

@end

@implementation MYMyCollectNewsListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;

    self.newsArray = NSMutableArray.new;
    [self getUserNewsCollectionsApi:YES];
    [self setupRefresh];
}

- (void)setupRefresh
{
    self.listTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getUserNewsCollectionsApi:YES];
    }];

    self.listTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        if (self.newsArray.count < 10) {
            [self.listTableView.mj_footer endRefreshing];
            return ;
        }
        self.page ++;

        [self getUserNewsCollectionsApi:NO];
    }];
    [self.listTableView.mj_header beginRefreshing];
    self.listTableView.mj_header.automaticallyChangeAlpha = YES;

}
#pragma amrk - POST /api/collect/queryArticleList查询资讯收藏列表
- (void)getUserNewsCollectionsApi:(BOOL)isReload
{
    if (isReload) {
        self.page = 1;
    }
    NSMutableDictionary *dic = NSMutableDictionary.new;
    dic[@"page"] = @(self.page);
    dic[@"limit"] = @(10);
    [self.newsArray removeAllObjects];
    [YQPNetWorkingManager sendPostRequest:kPOST_QUERY_INFORMATION_LIST_URL parameters:dic success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        self.model = [YQPNewsListModel mj_objectWithKeyValues:responseObject.data];
        [self.newsArray addObjectsFromArray:self.model.list];
        [self.listTableView reloadData];
        [self.listTableView.mj_header endRefreshing];
        [self.listTableView.mj_footer endRefreshing];

    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        [self.listTableView.mj_header endRefreshing];
        [self.listTableView.mj_footer endRefreshing];
    }];
}

-(void)setupView {
    [super setupView];
    kYQREGISTERTABLEVIEWNIBCELL(@"MYHomeSearchCell");
}
#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.newsArray.count;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    YQPNewsModel *model = nil;
    if (indexPath.row < self.newsArray.count) {
        model = self.newsArray[indexPath.row];
    }
    MYHomeSearchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYHomeSearchCell"];
    cell.contentLb.text = model.title;
    [cell.userHeaderIcon sd_setImageWithURL:[NSURL URLWithString:model.headImg] placeholderImage:IMAGE_NAMED(@"")];
    [cell.rightImageView sd_setImageWithURL:[NSURL URLWithString:model.informationImage] placeholderImage:IMAGE_NAMED(@"")];
    cell.userNmaeLb.text = model.name;
    cell.stateCountLb.text = [NSString stringWithFormat:@"%@ %@",kYQLS(@"用户点赞数"),model.likeNumber.length >0 ? model.likeNumber:@"0"];
    NSString *timeStamp = [YQPInsureValidate timeInStr:model.createTime];
    cell.timeLb.text = [YQPInsureValidate compareCurrentTime:timeStamp];
    return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 97;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //YQPNewsModel *model = self.newsArray[indexPath.row];
    YQPNewsModel *model = nil;
    if (indexPath.row < self.newsArray.count) {
        model = self.newsArray[indexPath.row];
    }
    switch ([model.informationCategoryId integerValue]) {
        case 1:
        {
            MYNewsDetailsHotelVC *detailsHotelVC = [[MYNewsDetailsHotelVC alloc] init];
            detailsHotelVC.informationId = model.informationId;
            detailsHotelVC.informationCategoryId = @"1";
            kYQPUSH(detailsHotelVC);
        }
            break;
        case 2:
        {
            MYNewsDetailsScenicspotVC * scenicspotVC = [[MYNewsDetailsScenicspotVC alloc] init];
            scenicspotVC.informationCategoryId = @"2";
            scenicspotVC.informationId = model.informationId;
            kYQPUSH(scenicspotVC);
        }
            break;
        case 3:
        {
            MYNewsDetailsActionVC *actionVC = [[MYNewsDetailsActionVC alloc] init];
            actionVC.informationId = model.informationId;
            actionVC.informationCategoryId = @"3";
            kYQPUSH(actionVC);
        }
            break;

        default:
        {
//            MYNewsDetailsHotTopicVC *vc = [[MYNewsDetailsHotTopicVC alloc] init];
//            vc.informationId = model.informationId;
//            vc.informationCategoryId = @"1";
//            kYQPUSH(vc);
        }
            break;
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end


@end
